# @Time : 2020/8/24 15:31
# @Author : Fioman 
# @Phone : 13149920693
import cv2 as cv
import numpy as np

"""
图像平移,表示的是计算平移后的坐标,然后计算它之前的坐标的像素值赋值给新坐标即可.
M = [[1,0,tx],
     [0,1,ty]]
原来的坐标是x1,y1,t ->  (x1+tx,y1+ty) 后面补充的不要了.
通过这个平移矩阵,可以计算出来平移后的像素位置,然后根据这个像素位置去计算平移后的值. 
"""
image = cv.imread("pic/03.png")
cv.imshow("Original", image)
# 得到平移矩阵
M = np.float32([[1, 0, 25], [0, 1, 50]])
shifted = cv.warpAffine(image, M, (image.shape[1], image.shape[0]))
cv.imshow("Shifed Down And Right", shifted)

M = np.float32([[1, 0, -50], [0, 1, -50]])
shifted = cv.warpAffine(image, M, (image.shape[1], image.shape[0]))
cv.imshow("Shifed Up And Left", shifted)

cv.waitKey(0)


def translate(src, x, y):
    """平移一个图像,x表示横向移动距离,y表示纵向移动距离.x向右为正,y向下为正"""
    M = np.float32([[1, 0, x], [0, 1, y]])
    shifted = cv.warpAffine(image, M, (image.shape[1], image.shape[0]))
    return shifted


image = cv.imread("pic/01.png")
translated = translate(image, 100, 100)
cv.imshow("Translated", translated)
cv.waitKey(0)

# 旋转,首先确定旋转中心点,旋转方向,逆时针方向为正值.

image = cv.imread("pic/02.png")
center = image.shape[1] / 2, image.shape[0] / 2
M = cv.getRotationMatrix2D(center, 45, 1.0)
rotated = cv.warpAffine(image, M, (image.shape[1], image.shape[0]))
cv.imshow("Rotated by 45 Degrees", rotated)

M = cv.getRotationMatrix2D(center, -90, 1.0)
rotated = cv.warpAffine(image, M, (image.shape[1], image.shape[0]))
cv.imshow("Roated by -90 Degrees", rotated)
cv.waitKey(0)

center = image.shape[1] / 2 - 50, image.shape[0] / 2 - 50
M = cv.getRotationMatrix2D(center, 100, 1.0)
rotated = cv.warpAffine(image, M, (image.shape[1], image.shape[0]))
cv.imshow("Roated not center by 100 degrees", rotated)
cv.waitKey(0)


def rotated(src, angle, center=None, scale=1.0):
    if center is None:
        center = src.shape[1] / 2, src.shape[0] / 2
    M = cv.getRotationMatrix2D(center, angle, scale)
    srcRotated = cv.warpAffine(src, M, (src.shape[1], src.shape[0]), scale)
    return srcRotated


center = image.shape[1] / 2 - 100, image.shape[0] / 2 - 100
rotated = rotated(image, 200, center, 1.0)
cv.imshow("Rotated center by 200 degrees", rotated)
cv.waitKey(0)

image = cv.imread("pic/04.png")
M = cv.getRotationMatrix2D((image.shape[1] / 2, image.shape[0] / 2), -30, 1.0)
rotated = cv.warpAffine(image, M, (image.shape[1], image.shape[0]))

b, g, r = rotated[254, 335]
print("R = {},G = {},B = {}".format(r, g, b))

M = cv.getRotationMatrix2D((image.shape[1] / 2, image.shape[0] / 2), 110, 1.0)
rotated = cv.warpAffine(image, M, (image.shape[1], image.shape[0]))
b, g, r = rotated[136, 312]
print("R = {},G = {},B = {}".format(r, g, b))

M = cv.getRotationMatrix2D((50, 50), 88, 1.0)
rotated = cv.warpAffine(image, M, (image.shape[1], image.shape[0]))
b, g, r = rotated[10, 10]
print("R = {},G = {},B = {}".format(r, g, b))

"""
图像缩放的时候,要注意缩放比例. 宽度和高度的缩放都要注意比例.
宽高要一起缩放,并且缩放的时候,要注意像素的插值方式.
1> 双线性插值
2> 最近邻插值方法
3> 最近区域插值法
"""

image = cv.imread("pic/04.png")
newHeight = 500
newWidth = int(image.shape[1] / image.shape[0] * newHeight)
resize = cv.resize(image, (newWidth, newHeight), interpolation=cv.INTER_AREA)

cv.imshow("Resized_Height:{}".format(newHeight), resize)
cv.waitKey(0)


def my_resize(image, width=None, height=None, inter=cv.INTER_AREA):
    h, w = image.shape[:2]
    if width is None and height is None:
        return image

    if width is None:
        newHeight = int(height)
        newWidth = int(w / h * newHeight)
    else:
        newWidth = int(width)
        newHeight = int(h / w * newWidth)
    dim = newWidth, newHeight

    resized = cv.resize(image, dim, interpolation=inter)

    return resized


# 使用各种插值方法处理一张图片
interMethods = [cv.INTER_NEAREST, cv.INTER_LINEAR, cv.INTER_AREA, cv.INTER_CUBIC, cv.INTER_LANCZOS4]

for method in interMethods:
    resized = my_resize(image, width=image.shape[1] * 0.8, inter=method)
    cv.imshow("Method:{}".format(str(method)), resized)
    cv.waitKey(0)

image = cv.imread("pic/05.png")
h, w = image.shape[:2]
newWidth = 100
newHeight = int(h / w * newWidth)
resized = cv.resize(image, (newWidth, newHeight), interpolation=cv.INTER_NEAREST)
b, g, r = resized[74, 20]
print("R = {},G = {},B = {}".format(r, g, b))

newWidth = w*2
newHeight = h*2
resized = cv.resize(image,(newWidth,newHeight),interpolation=cv.INTER_CUBIC)
b,g,r = resized[367,170]
print("R = {},G = {},B = {}".format(r,g,b))


"""
flip(src,0) # 垂直翻转
flip(src,1) # 水平翻转
"""

cv.imshow("Original",image)
flippedX = cv.flip(image,1)
flippedY = cv.flip(image,0)
flippedXY = cv.flip(image,-1)

cv.imshow("FlippedX",flippedX)
cv.imshow("FlippedY",flippedY)
cv.imshow("FlippedXY",flippedXY)
cv.waitKey(0)

"""
crop an image.  切片截取一幅图像
"""
image = cv.imread("pic/02.png")
cv.imshow("Original",image)
someCrop = image[20:50,20:50]
cv.imshow("SomeCrop",someCrop)
cv.waitKey(0)


image = cv.imread("pic/07.png")
A = image[85:250,85:220]
B = image[90:450,0:290]
C = image[124:212,225:380]
D = image[173:235,13:81]
cv.imshow("A",A)
cv.imshow("B",B)
cv.imshow("C",C)
cv.imshow("D",D)

cv.waitKey(0)













































